package cn.farm.farm.Util.file;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class OssUtil {


    private static String domain,endpoint,accessKeyId,accessKeySecret;

    @Value("${aliyunoss.domain}")
    public void setDomain(String domain){
        OssUtil.domain = domain;
    }

    @Value("${aliyunoss.endpoint}")
    public void setendpoint(String endpoint){
        OssUtil.endpoint = endpoint;
    }

    @Value("${aliyunoss.accessKeyId}")
    public void setaccessKeyId(String accessKeyId){
        OssUtil.accessKeyId = accessKeyId;
    }

    @Value("${aliyunoss.accessKeySecret}")
    public void setaccessKeySecret(String accessKeySecret){
        OssUtil.accessKeySecret = accessKeySecret;
    }


    //二进制 文件上传
    public static String uploadFile(String houzui,ObjectMetadata objectMetadata,MultipartFile file){
        // 创建OSSClient实例。
        OSS ossClient =  new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        PutObjectRequest putObjectRequest = null;
        String fileName= System.currentTimeMillis()+houzui;
        try {
            putObjectRequest = new PutObjectRequest("i-am-fine", fileName, multipartFileToInputStream(file));
            putObjectRequest.setMetadata(objectMetadata);
        } catch (Exception e) {
            log.error("multipartFile转化file 失败");
            e.printStackTrace();
        }

        ossClient.putObject(putObjectRequest);
        log.info("文件"+houzui+"上传成功："+fileName);
        ossClient.shutdown();
        return domain+fileName;
    }


    //上传视频
    public static String uploadVideo(MultipartFile file){

        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
        ObjectMetadata metadata = new ObjectMetadata();
        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        metadata.setObjectAcl(CannedAccessControlList.PublicRead);
        // putObjectRequest.setMetadata(metadata);

        return uploadFile(".mp4",metadata,file);
    }

    //上传图片
    public static String uploadImage(MultipartFile file){

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setObjectAcl(CannedAccessControlList.PublicRead);
        return uploadFile(".png",metadata,file);

    }

    //multipartFile转化输入流
    public static InputStream multipartFileToInputStream(MultipartFile file) throws IOException{

        InputStream ins = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            ins = file.getInputStream();
        }
        return ins;

    }

    //multipartFile转化file
    public static File multipartFileToFile(MultipartFile file) throws IOException{

        File toFile = null;
        if (file==null||file.equals("") || file.getSize() <= 0) {
            return null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    private static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //多张图片的处理
    public static List<String> dealImage(List<MultipartFile> fileList){
        List<String> list  = new ArrayList<>();
        if (fileList.size()==0)
            return list;
        String imagepath;
        for (MultipartFile multipartFile : fileList) {
            imagepath = uploadImage(multipartFile);
            list.add(imagepath);
        }
        return list;
    }

    //多张图片的处理
    public static List<String> dealImage(MultipartFile[] fileList){
        List<String> list  = new ArrayList<>();
        if (fileList.length==0)
            return list;
        String imagepath;
        for (MultipartFile multipartFile : fileList) {
            imagepath = uploadImage(multipartFile);
            list.add(imagepath);
        }
        return list;
    }


}
